package com.sweet.fourtimeoa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sweet.fourtimeoa.mapper.SystemMapper;
import com.sweet.fourtimeoa.pojo.dto.DocumentList;
import com.sweet.fourtimeoa.pojo.dto.SystemUserQueryDto;
import com.sweet.fourtimeoa.pojo.entity.Departments;
import com.sweet.fourtimeoa.pojo.entity.Roles;
import com.sweet.fourtimeoa.pojo.vo.DepartmentVo;
import com.sweet.fourtimeoa.pojo.vo.PageVo;
import com.sweet.fourtimeoa.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    private SystemMapper systemMapper;

    @Override
    public PageVo<Roles> queryRolesList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Roles> list =(Page<Roles>)systemMapper.queryRolesList();
        PageVo<Roles> pageVo = new PageVo<>();
        pageVo.setPageNum(list.getPageNum());
        pageVo.setPageSize(list.getPageSize());
        pageVo.setTotal(list.getTotal());
        pageVo.setPages(list.getPages());
        pageVo.setList(list.getResult());
        return pageVo;
    }

    @Override
    public void insertRoles(String roleName1) {
        systemMapper.insertRoles(roleName1);
    }

    @Override
    public void deleteRoles(Integer roleId) {
        systemMapper.deleteRoles(roleId);
    }

    @Override
    public void updateRoles(Roles roles) {
        systemMapper.updateRoles(roles);
    }

    @Override
    public Roles getByUpdateRoles(Integer roleId) {
        return systemMapper.getByUpdateRoles(roleId);
    }

    /**
     * 查询所有部门
     * @return
     */
    @Override
    public List<DepartmentVo> getAllDepartment() {
        List<DepartmentVo> departmentVos = new ArrayList<>();
            departmentVos.add(getDepartment(1));
        return departmentVos;
    }
    public  DepartmentVo getDepartment(Integer parentId){
        DepartmentVo departmentVo = new DepartmentVo();
        departmentVo.setValue(parentId);
        List<DepartmentVo> children = new ArrayList<>();
        departmentVo.setLabel(systemMapper.getDepartmentsById(parentId).getDepartmentName());
        List<Departments> departments = systemMapper.getDepartmentsByParentId(parentId);
        if (departments.isEmpty()){
            return departmentVo;
        }
        for (Departments department : departments) {
            children.add(getDepartment(department.getId()));
        }
        departmentVo.setChildren(children);
        return departmentVo;
    }

    @Override
    public Departments getDepartmentsById(Integer id) {
        return systemMapper.getDepartmentsById(id);
    }

    @Override
    public void updateDepartments(Departments departments) {
        systemMapper.updateDepartments(departments);
    }

    @Override
    public void insertDepartment(Departments departments) {
        systemMapper.insertDepartment(departments);
    }

    @Override
    public void deleteDepartment(Integer id) {
        systemMapper.deleteDepartment(id);
    }


}
